package com.xdhl.fu.fim.price.strategy;

import com.xdhl.fu.common.core.constant.flightinfo.FlightInfoConstants;
import com.xdhl.fu.common.core.utils.MathUtils;
import com.xdhl.fu.common.core.utils.StringUtils;
import com.xdhl.fu.common.redis.service.RedisService;
import com.xdhl.fu.flight.info.api.domain.FlightInfo;
import com.xdhl.fu.flight.info.api.domain.FlightSchedule;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 热门城市价格变化策略
 *
 * @author 朱力维
 * @time 2022/6/3 18:17
 */
@Component
public class HotCityEffectPriceStrategy implements PriceCalcStrategy {

    /**
     * 热门城市缓存键
     */
    public static final String CONFIG_KEY = "sys_config:hot-city.discount";

    @Resource
    private RedisService redisService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public int order() {
        return 2;
    }

    @Override
    public PriceCalcStrategy calcEconomy(FlightInfo flightInfo, FlightSchedule schedule) {
        String discount = redisService.getStringValue(CONFIG_KEY).split("\"")[1];
        if (StringUtils.isNotBlank(discount)) {
            Double price = MathUtils.priceMultip(Double.parseDouble(discount), schedule.getEconomyPrice());
            schedule.setEconomyPrice(price);
        }
        return this;
    }

    @Override
    public PriceCalcStrategy calcBusiness(FlightInfo flightInfo, FlightSchedule schedule) {
        String discount = redisService.getStringValue(CONFIG_KEY).split("\"")[1];
        if (StringUtils.isNotBlank(discount)) {
            Double price = MathUtils.priceMultip(Double.parseDouble(discount), schedule.getBusinessPrice());
            schedule.setBusinessPrice(price);
        }
        return this;
    }

    @Override
    public PriceCalcStrategy calcHeader(FlightInfo flightInfo, FlightSchedule schedule) {
        String discount = redisService.getStringValue(CONFIG_KEY).split("\"")[1];
        if (StringUtils.isNotBlank(discount)) {
            Double price = MathUtils.priceMultip(Double.parseDouble(discount), schedule.getHeadPrice());
            schedule.setHeadPrice(price);
        }
        return this;
    }

    @Override
    public boolean enable(FlightInfo flightInfo, FlightSchedule schedule) {
        BoundHashOperations<String, String, Object> boundHashOperations = stringRedisTemplate.boundHashOps(FlightInfoConstants.HOT_CITY_CACHE_KEY);
        return "1".equals(boundHashOperations.get(flightInfo.getEndStation()));
    }

}
